<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.springframework.org/schema/mvc">
<head>
    <meta charset="UTF-8">
    <title>课程管理</title>
    <!--<link rel="stylesheet" href="layui/css/layui.css">-->
</head>
<body>
<!--面包屑导航-->
<div class="layui-card" style="padding: 15px">
    <div class="layui-breadcrumb">
        <a href="javascript:;">首页</a>
        <a href="javascript:;">
            <cite>课程管理</cite>
        </a>
    </div>
</div>

<div >
    <ul >
        <li style="float: left;margin-left: 10px">
            <button class="layui-btn layui-btn-primary" onclick="openEdit()" style="" id="edtBtn">打开编辑状态</button>
        </li>
        <li style="float: left;margin-left: 200px;">
           <H1 id="zhou">第1周</H1>
        </li>

    </ul>
    <div style="clear: both"></div>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
        <table id="course_table" lay-filter="course" class="layui-table" width="100%">
            <thead class="layui-table-header">
            <tr>
                <th></th>
                <th>星期一</th>
                <th>星期二</th>
                <th>星期三</th>
                <th>星期四</th>
                <th>星期五</th>
            </tr>
            </thead>
            <tbody class="layui-table-body">
            <tr id="c1" >
                <td>1</td>
                <td class="cla" courseTime="1,1"></td>
                <td class="cla" courseTime="2,1"></td>
                <td class="cla" courseTime="3,1"></td>
                <td class="cla" courseTime="4,1"></td>
                <td class="cla" courseTime="5,1"></td>
            </tr>
            <tr id="c2">
                <td>2</td>
                <td class="cla" courseTime="1,2"></td>
                <td class="cla" courseTime="2,2"></td>
                <td class="cla" courseTime="3,2"></td>
                <td class="cla" courseTime="4,2"></td>
                <td class="cla" courseTime="5,2"></td>
            </tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>

            <tr id="c3">
                <td>3</td>
                <td class="cla" courseTime="1,3"></td>
                <td class="cla" courseTime="2,3"></td>
                <td class="cla" courseTime="3,3"></td>
                <td class="cla" courseTime="4,3"></td>
                <td class="cla" courseTime="5,3"></td>
            </tr>
            <tr id="c4">
                <td>4</td>
                <td class="cla" courseTime="1,4"></td>
                <td class="cla" courseTime="2,4"></td>
                <td class="cla" courseTime="3,4"></td>
                <td class="cla" courseTime="4,4"></td>
                <td class="cla" courseTime="5,4"></td>
            </tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>

            <tr id="c5">
                <td>5</td>
                <td class="cla" courseTime="1,5"></td>
                <td class="cla" courseTime="2,5"></td>
                <td class="cla" courseTime="3,5"></td>
                <td class="cla" courseTime="4,5"></td>
                <td class="cla" courseTime="5,5"></td>
            </tr>

            </tbody>
        </table>
    </div>
        <div class="layui-tab-item">

        </div>
    </div>

</div>
<!--编辑弹出框-->
<script type="text/html" id="addClasseLayer">
    <div id="course_detail" style="width: 400px">
        <table class="layui-table" lay-skin="nob" id="sel">
            <tbody class="layui-table-body">
            <tr>
                <td>课程名</td>
                <td>
                    <div class="layui-form">
                        <input v-model="courseName" type="text"  class="layui-input" >
                    </div>
                </td>
            </tr>
            <tr>
                <td>课程班级</td>
                <td>
                    <div  >
                        <select v-model="classeId"  style="width: 300px;height: 30px">
                            <option v-bind:value="op.classeId"  v-for="op in classe">{{op.classeNumber}}</option>
                        </select>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</script>

<script src="/js/axios.min.js" th:src="@{/js/axios.min.js}"></script>
<script src="/js/vue.min.js" th:src="@{/js/vue.min.js}"></script>
<script>
   var isEdit=false;
   jiansuanzhousu();
   function openEdit(){
       if(isEdit){
           document.getElementById("edtBtn").style=""
           document.getElementById("edtBtn").innerText="打开编辑状态"
           isEdit=false;
       }else{

           document.getElementById("edtBtn").style="background: red"
           document.getElementById("edtBtn").innerText="编辑中......"
           isEdit=true
       }
   }

    layui.use(['layer'], function () {
       var layer = layui.layer;
   });

    axios.get("/course/AllCourse")
        .then(function (response) {
            var data = response.data.data;
            for (var i = 0; i < data.length; i++) {
                var time = data[i].courseTime
                var aarrTime2 = time.split(",")
                var tr = document.getElementById("c" + aarrTime2[1])
                var tds = tr.getElementsByTagName("td")
                var jie = parseInt(aarrTime2[0])
                tds[jie].innerHTML = data[i].courseNumber;
                tds[jie].setAttribute("courseId",data[i].courseId)
            }
            console.log(data);
        })
        .catch(function (error) {
            console.log(error);
        });

    var tds = document.getElementsByClassName("cla");
    console.log(tds.length + "1231")
    for (var ii = 0; ii < tds.length; ii++) {
        tds[ii].addEventListener("click", click);
    }
   function click(even) {
             if(isEdit){
                 addClasseLayer(even);
             }  else{
                 qiandao(even)
            }
        }
    
    function addClasseLayer(even) {
       // console.log(even)
        var courseId=even.target.getAttribute("courseId")
        var vue;
        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: true
            ,area: '410'
            ,shade: 0.8
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btn: ['确定','删除' ,'取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: document.getElementById("addClasseLayer").innerHTML
            ,success: function(layero, index){
                 initOPS(even.target.getAttribute("classeId"),even.target.innerText);
            }
            ,btn1: function(index, layero){
                if(courseId!=null){
                    layer.msg("修改中")
                    var classeId=
                    console.log(vue.classeId)
                    console.log(vue.courseName)
                    axios.get("/course/updata",{
                        params:{
                            classeId:vue.classeId,
                            courseNumber:vue.courseName,
                            courseId:courseId,
                        }
                    })
                        .then(function (response) {
                            if(response.data.code==200) {
                                even.target.innerText=vue.courseName
                                layer.msg("成功")
                                layer.closeAll()
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }else{
                    //添加
                    axios.get("/course/insert",{
                        params:{
                            classeId:vue.classeId,
                            courseNumber:vue.courseName,
                            courseTime:even.target.getAttribute("courseTime"),
                        }
                    })
                        .then(function (response) {
                            if(response.data.code==200) {
                                even.target.innerText=vue.courseName
                                layer.msg("成功")
                                layer.closeAll()
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }

            }
            ,btn2:function () {
                console.log("-----------")
                axios.get("/course/delete",{
                    params:{
                        courseId:courseId,
                    }
                })
                    .then(function (response) {
                        if(response.data.code==200) {
                            even.target.innerText=""
                            even.target.removeAttribute("classeId")
                            layer.msg("成功")
                            layer.closeAll()
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }
        });

//初始化ops
        function initOPS(classeId,courseName) {
            axios.get("/d/classe/findAllClasseNoPage")
                .then(function (response) {
                    vue =  new Vue({
                        el:"#sel",
                        data:{
                            classe:response.data.data,
                            classeId:classeId,
                            courseName:courseName
                        }
                    })
                    return vue;
                })
                .catch(function (error) {
                    console.log(error);
                });

        }
    }

    function qiandao(even) {
        layer.open({
            type: 1
            , title: false //不显示标题栏
            , closeBtn: true
            , area: '410'
            , shade: 0.8
            , id: 'LAY_layuipro' //设定一个id，防止重复弹出
            , btn: ['确定', '取消']
            , btnAlign: 'c'
            , moveType: 1 //拖拽模式，0或者1
            , content: "开始签到！！！！！"
            , success: function (layero, index) {

            }
            , btn1: function (index, layero) {

                var courseId=even.target.getAttribute("courseId")
                axios.get("  /course/beginRegist",{
                    params:{
                        courseId:courseId,
                        zhou:document.getElementById("zhou").innerText
                    }
                })
                    .then(function (response) {
                        if(response.data.code==200) {

                            layer.msg("成功")
                            axios.get('/register/begingRegister', {
                                params: {
                                    activityId: response.data.activityId
                                }
                            })
                                .then(function (response) {
                                    if(response.data.code==200){
                                        //跳转到
                                        layer.closeAll()
                                        $("#layui-body").load("/html/register")
                                    }else{
                                        layer.msg(response.data.message)
                                    }
                                })
                                .catch(function (error) {
                                    console.log(error);
                                });
                        }else{
                            layer.msg("!!!")
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }
            , btn2: function () {
                console.log("删除")
            }

        })
    }

    //计算周数
    function  jiansuanzhousu() {
        //一学期的开始 第一周的星期一
        var xinqi=["一",'二','三','四','五','六','天']
        var s1 = '2019-02-25';
        s1 = new Date(s1.replace(/-/g, "/"));
        s2 = new Date();//当前日期：2017-04-24
        var days = s2.getTime() - s1.getTime();
        var time = parseInt(days / (1000 * 60 * 60 * 24));
      //  document.write(time/7);
        console.log(time)
        console.log(parseInt(time/7)+1);
        document.getElementById("zhou").innerText="第"+parseInt((time/7)+1)+"周 星期"+xinqi[time%7]
    }
</script>


</body>
</html>